package com.tianyi.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * @Author tianyi
 * @Description 异常的拦截处理
 * @Date 2022 - 11 - 05
 **/
@ControllerAdvice
@Slf4j
public class ApplicationExceptionAdvice<T>  {

    @ExceptionHandler(ApplicationException.class)
    @ResponseBody
    public WebResponse<T> handleApplicationException(ApplicationException ex ){
       log.warn("错误", ex.getMessage(), ex);
       return WebResponse.fail(ex.getErrorCode() , ex.getMessage());
    }


    @ExceptionHandler(Exception.class)
    public WebResponse<T> handleException(Exception ex){
        log.warn("error", ex.getMessage());
        return WebResponse.fail(500, ex.getMessage());
    }
}
